<?
echo("
<STYLE type=text/css>
A:link {COLOR: blue;text-decoration:none;}
A:visited {COLOR: blue}
A:hover {COLOR: green}
BODY {COLOR: black} 
</STYLE>
");


session_start(); 

define('T_LOG','Log');

$g_width   = $_SESSION['g_width'];
$g_filter  = $_SESSION['g_filter'];
$g_graph   = $_SESSION['g_graph'];

$s_year   = $_SESSION['s_year'];
$s_month  = $_SESSION['s_month'];
$s_day    = $_SESSION['s_day'];

$temp                 = $_POST['s_year'];
if($temp)$s_year      = $_POST['s_year'];
if(!$s_year)$s_year   = date('Y');

$temp                 = $_POST['s_month'];
if($temp)$s_month     = $_POST['s_month'];
if(!$s_month)$s_month = date('m');
$s_month += 0;

$temp                 = $_POST['s_day'];
if($temp)$s_day       = $_POST['s_day'];
if(!$s_day)$s_day     = date('d');
$s_day += 0;

$gw  = $_POST['g_width'];
if($gw) $g_width = $gw;

$gf  = $_POST['g_filter'];
if($gf) $g_filter = $gf;
if($g_filter == 99)$g_filter = 0;

$gg  = $_POST['g_graph'];
if($gg) $g_graph = $gg;

if(!$g_graph)$g_graph = 'day';
if(!$g_width)$g_width = 2000;

$dir  = $_GET['dir'];
if($dir == 'next' && $s_day < 30) $s_day++;
if($dir == 'prev' && $s_day > 1) $s_day--;

$_SESSION['g_width']  = $g_width ;
$_SESSION['g_filter'] = $g_filter ;
$_SESSION['g_graph']  = $g_graph ;

$_SESSION['s_year']   = $s_year ;
$_SESSION['s_month']  = $s_month ;
$_SESSION['s_day']    = $s_day ;

$logf       = array();
$dayEnergy  = array();
$distYear   = array();
$distMonth  = array();
$distDay    = array();
$distHour   = array();
$distMinute = array();


  system("ls bess-20*.txt > bess-log-list.txt");
  $in = fopen("bess-log-list.txt","r");
  $nlogf = 0;
  if($in)
    {
      while (!feof($in))
	{
	  $nlogf++;
          $file = trim(fgets($in));
	  $logf[$nlogf] = $file;
 	  $temp = count(file($file)); 
          $temp2 = str_replace("bess-", "", $file);
          $temp2 = str_replace(".txt", "", $temp2);
          $time  = explode("-", $temp2);
          $year  = $time[0];
          $month = $time[1];
          $day   = $time[2];
          $eYear[$year]++;
          $eMonth[$month]++;
          $eDay[$day]++;
          $dayEnergy[$nlogf] = $temp;
          $distDay[$year][$month][$day] = $temp;
          $distMonth[$year][$month]    += $temp;  
          $distYear[$year]             += $temp;          
        }
    }


$g_log = 'bess-'.$s_year.'-'.$s_month.'-'.$s_day.'.txt';
//echo("Antal dagar: $nlogf log=$g_log size=$g_width filter=$g_filter graph=$g_graph<br>");
//echo("g=$g_graph $s_year $s_month $s_day<br>");

if($g_log)
{
  $file = $g_log;
  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
	{
	  $row = fgets($in);
	  sscanf($row,"%d, %d, %d, %d, %d, %d, %d",$year,$month,$day,$hour,$minute,$second,$power);
	  //echo("$year,$month,$day,$hour,$minute,$second,$power <br>");
          $distHour[$year][$month][$day][$hour]++;
          $distMinute[$year][$month][$day][$hour][$minute]++;
        }
    }
   fclose($in);
}

  $current = 'no data';
  $in = fopen("bess.html","r");
  if($in)
    {
	  $current = fgets($in);
    }
   fclose($in);
//=========================================================================
//------------------------------------------------
?>

<html>
  <head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  function update()
{
  $.get("ajax.php", {user: '1'}, function(data){ $("#getmytime").html(data);});  
  setTimeout('update()', 1000);
}
$(document).ready(function(){update();});
</script>


    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1.0', {'packages':['corechart']});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
      var data = google.visualization.arrayToDataTable([
          ['Hour', 'Power'],
<?php
//------------------------------------------------
if($g_graph == 'data')
{
  $ne = 0;
  $file = $g_log;
  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
	{
          $ne++;
	  $row = fgets($in);
	  sscanf($row,"%d, %d, %d, %d, %d, %d, %d",$year,$month,$day,$hour,$minute,$second,$power);
          $distHour[$year][$month][$day][$hour]++;
          $distMinute[$year][$month][$day][$hour][$minute]++;
          if($g_filter > 0)
          {
            $nt++;
            $ap = $ap + $power;
            if($nt == $g_filter)
            {
	       $ap = $ap/$g_filter;
               echo("['$hour-$minute-$second',$ap],");
               $nt = 0; $ap = 0;
            }
          }
          else
           echo("['$hour-$minute-$second',$power],");
        }
      echo("['1999',1]");
      echo("]);");
    }
   fclose($in);
$temp = $distDay[$year][$month][$day]/1000;
echo("
        var options = {'title':'Datum: $year - $month - $day [$temp kWh]',
                       'width':$g_width,
                       'height':500,
                       'fontSize':12,
'chartArea':{left:100,top:30,width:\"50%\",height:\"70%\"}
                      }
      
    ");
}

if($g_graph == 'tot')
{
      for($ii=2013;$ii<2030;$ii++)
      {
           $temp = $distYear[$ii]/1000;
           if(!$temp)$temp = 0;
           if($ii<2030)echo("['$ii',$temp],");
      }  
      $temp = $distYear[2030]/1000; 
      if(!$temp)$temp = 0;
      echo("['2030',$temp]");
      echo("]);");
    echo("
        var options = {'title':'Tot',
                       'width':$g_width,
                       'height':500,
                       'fontSize':12,
'chartArea':{left:100,top:30,width:\"50%\",height:\"70%\"}
                      }
      
    ");
}

if($g_graph == 'year')
{
      for($ii=1;$ii<12;$ii++)
      {
           $temp = $distMonth[$s_year][$ii]/1000;
           if(!$temp)$temp = 0;
           if($ii<12)echo("['$ii',$temp],");
      }  
      $temp = $distMonth[$s_year][12]/1000; 
      if(!$temp)$temp = 0;
      echo("['12',$temp]");
      echo("]);");
    echo("
        var options = {'title':'$s_year',
                       'width':$g_width,
                       'height':500,
                       'fontSize':12,
'chartArea':{left:100,top:30,width:\"50%\",height:\"70%\"}
                      }
      
    ");
}

if($g_graph == 'month')
{
      for($ii=1;$ii<31;$ii++)
      {
           $temp = $distDay[$s_year][$s_month][$ii]/1000;
           if(!$temp)$temp = 0;
           if($ii<31)echo("['$ii',$temp],");
      }  
      $temp = $distDay[$s_year][$s_month][31]/1000; 
      if(!$temp)$temp = 0;
      echo("['31',$temp]");
      echo("]);");
    echo("
        var options = {'title':'$s_year $s_month',
                       'width':$g_width,
                       'height':500,
                       'fontSize':12,
'chartArea':{left:100,top:30,width:\"50%\",height:\"70%\"}
                      }
      
    ");
}

if($g_graph == 'day')
{
      for($ii=0;$ii<23;$ii++)
      {
           $temp = $distHour[$s_year][$s_month][$s_day][$ii]/1000;
           if(!$temp)$temp = 0;
           if($ii<23)echo("['$ii',$temp],");
      }  
      $temp = $distHour[$s_year][$s_month][$s_day][23]/1000; 
      if(!$temp)$temp = 0;
      echo("['23',$temp]");
      echo("]);");
    echo("
        var options = {'title':'$s_year $s_month $s_day',
                       'width':$g_width,
                       'height':500,
                       'fontSize':12,
'chartArea':{left:100,top:30,width:\"50%\",height:\"70%\"}
                      }
      
    ");
}

if($g_graph == 'data')
echo("
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
");
else
echo("
        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
        chart.draw(data, options);
");
//------------------------------------------------
?>
      }
    </script>
  </head>

  <body>

<?
//------------------------------------------------ 
echo("<div id=\"getmytime\">no data</div>");

echo("<a href='bessPi.php?dir=prev'> << </a> | ");
echo("<a href='bessPi.php?dir=next'> >> </a>");

//echo("$current<br> ");
//echo("<a href='bessPi.php?g_graph=tot'>totalt</a> | ");
//echo("<a href='bessPi.php?g_graph=year'>&aring;r</a> | ");
//echo("<a href='bessPi.php?g_graph=month'>m&aring;nad</a> | ");
//echo("<a href='bessPi.php?g_graph=day'>dag</a> | ");
//echo("<a href='bessPi.php?g_graph=data'>detalj</a>");
//$energy = $ne/1000.; echo("<h2>Dagens f&ouml;rbrukning: $energy kWh </h2><br>"); 
//------------------------------------------------
?>

  <form action="bessPi.php" method="POST">

   <select name="g_graph">
<?
   $selected = ' ';
   if($g_graph == 'tot')$selected = 'selected';
   echo("<option $selected value=\"tot\">Total</option>");
   $selected = ' ';
   if($g_graph == 'year')$selected = 'selected';
   echo("<option $selected value=\"year\">&aring;r</option>");
   $selected = ' ';
   if($g_graph == 'month')$selected = 'selected';
   echo("<option $selected value=\"month\">m&aring;nad</option>");
   $selected = ' ';
   if($g_graph == 'day')$selected = 'selected';
   echo("<option $selected value=\"day\">dag</option>");
   $selected = ' ';
   if($g_graph == 'data')$selected = 'selected';
   echo("<option $selected value=\"data\">Detalj</option>");
?>
  </select>

   <select name="g_width">
<?
   $selected = ' ';
   if($g_width == 500)$selected = 'selected';
   echo("<option $selected value=\"500\">Small</option>");
   $selected = ' ';
   if($g_width == 1000)$selected = 'selected';
   echo("<option $selected value=\"1000\">Medium</option>");
   $selected = ' ';
   if($g_width == 2000)$selected = 'selected';
   echo("<option $selected value=\"2000\">Large</option>");
   $selected = ' ';
   if($g_width == 10000)$selected = 'selected';
   echo("<option $selected value=\"10000\">XL</option>");
   $selected = ' ';
   if($g_width == 20000)$selected = 'selected';
   echo("<option $selected value=\"20000\">XXL</option>");
?>
  </select>
   <select name="g_filter">
<?
   $selected = ' ';
   if($g_filter == 0)$selected = 'selected';
   echo("<option $selected value=\"99\">0</option>");
   $selected = ' ';
   if($g_filter == 2)$selected = 'selected';
   echo("<option $selected value=\"2\">2</option>");
   $selected = ' ';
   if($g_filter == 5)$selected = 'selected';
   echo("<option $selected value=\"5\">5</option>");
   $selected = ' ';
   if($g_filter == 10)$selected = 'selected';
   echo("<option $selected value=\"10\">10</opton>");
   $selected = ' ';
   if($g_filter == 100)$selected = 'selected';
   echo("<option $selected value=\"100\">100</opton>");
?>
  </select>
  <select name="s_year">
<?
//------------------------------------------------ 
  for($ii=2013;$ii<=2050;$ii++)
  {
    $temp = $eYear[$ii];
    $selected = ' ';
    if($s_year == $ii)$selected = 'selected';
    if($eYear[$ii])
       echo("<option $selected value=\"$ii\">$ii</option>");
  }//------------------------------------------------
?>
  </select>

   <select name="s_month">
<?
//------------------------------------------------ 
  for($ii=1;$ii<=12;$ii++)
  {
    $temp = $eMonth[$ii];
    $selected = ' ';
    if($s_month == $ii)$selected = 'selected';
    if($eMonth[$ii])
       echo("<option $selected value=\"$ii\">$ii</option>");
  }//------------------------------------------------
?>
  </select>

   <select name="s_day">
<?
//------------------------------------------------ 
  for($ii=1;$ii<=31;$ii++)
  {
    $temp = $eDay[$ii];
    $selected = ' ';
    if($s_day == $ii)$selected = 'selected';
    if($eDay[$ii])
       echo("<option $selected value=\"$ii\">$ii</option>");
  }//------------------------------------------------
?>
  </select>

  <input type="submit" value="v&auml;lj" name="submit">
  </form>


 <div id="chart_div" style="width:400; height:300"></div>
  </body>
</html>
